#!/bin/bash


#
# Flow stability for dynamic community detection https://arxiv.org/abs/2101.06131v2
#
# Copyright (C) 2021 Alexandre Bovet <alexandre.bovet@maths.ox.ac.uk>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


# run scripts for the mice dataset: 1) compute laplacian and transition matrices
# 2) compute autocovariances and 3) perform clustering



# numpy/scipy should use 1 core since we have a multiprocessing script
export OPENBLAS_NUM_THREADS=4

NCPU=8


JNAME="micenet_2017_460"

NETFILENAME="micenet_2017_02_28_to_2017_05_01.pickle"

RUNDIR="$HOME/rundir"

DATADIRBASE="$HOME/datadir"

mkdir $DATADIRBASE

LAPTRANSDIR="$DATADIRBASE/lapl_transmat"

LAPINTERTRANSDIR="$DATADIRBASE/lapl_intertransmat"

TRANSMATDIR="$DATADIRBASE/transmatgrid"

INTEGDIR="$DATADIRBASE/integralgrid"


CLUSTSDIRI="$DATADIRBASE/clustersI"
CLUSTSDIRT="$DATADIRBASE/clustersT"


CLUSTNUMREPEAT=50

NETNAME="micenet2017"

# laplacians and transition matrices resolution grid
SLICELENGTH="$((60*60))" # slice length = 1 hour


# final grid resolution in unit of slices
INT_LENGTH="$((7*24))" # one week

mkdir $LAPTRANSDIR
mkdir $LAPINTERTRANSDIR
mkdir $TRANSMATDIR
mkdir $CLUSTSDIRI
mkdir $CLUSTSDIRT
mkdir $INTEGDIR


# waiting times 
# TAUWS=(1 60 "$((60*60))" "$((60*60*24))" "$((60*60*24*7))" 0.1 8 28800 18000 4234000)


# compute laplacians and intertransition matrices
python3 -u $RUNDIR/run_laplacians_transmat.py \
        --datadir $DATADIRBASE \
        --savedir $LAPINTERTRANSDIR \
        --net_filename $NETFILENAME \
        --net_name $NETNAME \
        --not_expm_transmat \
        --slice_length $SLICELENGTH \
        --ncpu $NCPU \
        --tau_w_list ${TAUWS[*]} \
        --save_slice_trans \
        --save_inter_trans \
        --compress_inter_trans \
        --t0 0 \
        > output_${JNAME}_lptm.txt 2> error_${JNAME}_lptm.txt
#         
        
# # compute autocov_grid        
python3 -u $RUNDIR/make_autocov_integral.py \
        --datadir $LAPINTERTRANSDIR \
        --savedir $INTEGDIR \
        --net_name $NETNAME \
        --int_length $INT_LENGTH \
        --ncpu $NCPU \
        --only_lin_transmats \
        --verbose \
        --dense_T_array \
        > output_${JNAME}_intg.txt 2> error_${JNAME}_intg.txt
# 
# 
# 
# # compute clustering of integral
# 
python3 -u $RUNDIR/run_clustering.py \
        --datadir $INTEGDIR \
        --savedir $CLUSTSDIRI \
        --ncpu $NCPU \
        --num_repeat $CLUSTNUMREPEAT \
        --net_name $NETNAME \
        > output_${JNAME}_clust.txt 2> error_${JNAME}_clust.txt

